package com.quanta.edaily.Service.impl;

import com.quanta.edaily.Service.CommentsService;
import com.quanta.edaily.bean.contants.ResultCode;
import com.quanta.edaily.config.exception.ServiceException;
import com.quanta.edaily.mapper.CommentsMapper;
import com.quanta.edaily.pojo.Comments;
import com.quanta.edaily.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class CommentsServiceImpl implements CommentsService {
    @Resource
    CommentsMapper commentsMapper;

    @Override
    public int insertFirst(Comments comments) {
        return commentsMapper.insert(comments);
    }

    @Override
    public int insertReply(Comments comments) {
        Comments par = getById(comments.getParentId());
        if(par==null) return 0;
        // 设置父级评论
        par.setReplyCount(par.getReplyCount()+1);
        updateById(par);
        // 根据父级设置articleId
        comments.setArticleId(par.getArticleId());
        // 根据父级设置祖宗评论
        Comments anc = getById(par.getAncestorId());
        if(anc!=null) {
            anc.setReplyCount(anc.getReplyCount()+1);
            updateById(anc);
            comments.setAncestorId(anc.getId());
        }
        else comments.setAncestorId(par.getId());
        comments.setUserId(TokenUtils.getCurrentUser().getId());
        return commentsMapper.insert(comments);
    }

    @Override
    public int deleteById(Integer id) {
        Comments comments = getById(id);
        if(comments==null||!Objects.equals(TokenUtils.getCurrentUser().getId(), comments.getUserId()))
            return 0;
        Comments anc = getById(comments.getAncestorId());
        Comments par = getById(comments.getParentId());
        // 设置回复量
        if (anc!=null) {
            anc.setReplyCount(anc.getReplyCount()-1);
            updateById(anc);
        }
        if (par!=null&&par!=anc) {
            par.setReplyCount(par.getReplyCount()-1);
            updateById(par);
        }
        return commentsMapper.deleteById(id);
    }

    @Override
    public int updateById(Comments comments) {
        int i=0;
        try {
            i = commentsMapper.updateById(comments);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(ResultCode.PARAM_ERROR, "参数错误");
        }
        return i;
    }

    @Override
    public Comments getById(Integer id) {
        Comments c1 = commentsMapper.getById(id);
        if(c1==null) return null;
        List<Comments> some = commentsMapper.getSome(c1.getArticleId());
        Collections.reverse(some); // 升序
        List<Comments> replies = some.stream().filter(c2 -> c1.getId().equals(c2.getAncestorId())).collect(Collectors.toList());
        // 找父级的user的id和name
        replies.forEach(reply -> {
            some.stream().filter(c3 -> c3.getId().equals(reply.getParentId())).findFirst().ifPresent((v -> {
                reply.setPUserId(v.getUserId()); // 有重名问题
                reply.setPUserName(v.getUserName());
//                reply.setPUserName(v.getUserId()==reply.getUserId()?null:v.getUserName());
            }));
        });
        c1.setChildren(replies);
        return c1;
    }

    @Override
    public List<Comments> getSome(Integer articleId) {
        // 查询所有，时间降序
        List<Comments> some = commentsMapper.getSome(articleId);
        // 查询一级评论
        List<Comments> ancList = some.stream().filter(comments -> comments.getAncestorId() == null).collect(Collectors.toList());
        Collections.reverse(some); // 反转一下实现一级评论时间降序，而二级评论升序
        // 设置回复
        for (Comments anc: ancList) {
            // 回复的列表
            List<Comments> replies = some.stream().filter(comments -> anc.getId().equals(comments.getAncestorId())).collect(Collectors.toList());
            // 找父级的user的id和name
            replies.forEach(reply -> {
                some.stream().filter(c1 -> c1.getId().equals(reply.getParentId())).findFirst().ifPresent((v -> {
                    reply.setPUserId(v.getUserId()); // 有重名问题
                    reply.setPUserName(v.getUserName());
                }));
            });
            anc.setChildren(replies);
        }
        return ancList;
    }
}
